<?php

namespace App\Kuafu\Bak\Command;

use Illuminate\Console\Command;

class ImportMysqlBakCommand extends Command
{
    protected $signature = 'bak:import-mysql';

    protected $description = '导入mysql备份文件';

    public function handle()
    {
        ini_set("memory_limit", "-1");

        // 开始月份
        $date = strtotime('2021-09-01');

        // 结束月份
        $endDate = strtotime('2022-12-31');

        while ($date <= $endDate) {
            $startTime = date('Ym', $date);

            print_r($startTime . '-------------');

            // 备份路径
            $files = glob("/data/backup/database/month/{$startTime}/*.sql");

            foreach ($files as $file) {
                print_r($file . '----');

                // 导入mysql
                exec("mysql -hrm-bp16jx32v02507gt4.mysql.rds.aliyuncs.com -uuser_jilian -pkvBFGxJfLDWVgGQ6 kf_rms_order_1 < {$file}");
            }

            $date = strtotime('+1 month', $date);
        }
    }

}
